import util from '@/utils/util'

// 菜单 顶栏
// export const menuHeader = []

// 菜单 侧边栏
// export const menuAside = []

/**
 * 将数据源处理为菜单树
 * @param arr
 * @returns {{header: Array, aside: Array}}
 */
export function getMenuData(arr) {
  let tree = { header: [], aside: [], funList: [] }
  if (arr) {
    arr.forEach(value => {
      // 存储功能按钮数据
      if (value.menuType === 'F') {
        tree.funList.push(value)
      }
      if (value.visible) {
        return
      }

      // 储存顶部(父节点)数据
      let arrElem = {
        path: value.pathUrl,
        title: value.menuTitle,
        icon: value.icon,
        remark: value.description,
        menuType: value.menuType,
        component: value.component
      }
      // arrElem.component = require(['@/views' + value.component])
      if (value.parentId === '0') {
        tree.header.push({ ...arrElem })
      }
      // 处理子节点数据
      arrElem['menuId'] = value.menuId
      arrElem['parentId'] = value.parentId
      tree.aside.push(arrElem)
    })
  }
  tree.aside = util.formatDataToTree(tree.aside)
  return tree
}

export default {
  install(vm, source) {
    const menu = getMenuData(source)
    vm.commit('kando/menu/headerSet', menu.header)
    vm.commit('kando/menu/asideSet', menu.aside)
    vm.commit('kando/menu/routerMenuSet', menu.aside)
    vm.commit('kando/search/init', menu.aside)
    localStorage.setItem('menuList', JSON.stringify(menu.aside))
    localStorage.setItem('permissions', JSON.stringify(menu.funList))
  }
}
